package day05.text04.controller;

import day05.text04.pojo.Goods;
import day05.text04.service.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Controller {

    private Service service = new Service();
    private Scanner sc = new Scanner(System.in);


    public void getGoodsList() throws IOException {
        ArrayList<Goods> goodsList = service.getGoodsList();
        for (Goods goods : goodsList) {
            System.out.println("商品名:" +goods.getName()+" | 价格："+goods.getPrice()+"| 重量:"+goods.getWeight());
        }
    }
    public void addGoods() throws IOException {
        ArrayList<Goods> goodsList = service.getGoodsList();
        while (true) {
            System.out.println("请输入编号");
            String id = sc.next();
            System.out.println("请输入商品名");
            String name = sc.next();
            boolean exists = service.isExists(id, name);
            if (exists) {
                System.out.println("请输入重量");
                String weight = sc.next();
                System.out.println("请输入价格");
                String price = sc.next();
                Goods g = new Goods(id, name, weight, price);
                goodsList.add(g);
                service.addGoods(goodsList);
                System.out.println("添加成功");
                break;
            }
        }
    }
    public void editGoods() throws IOException {
        while (true) {
            System.out.println("请输入编号");
            String id = sc.next();
            boolean exists = service.isExists(id);
            int indexById = service.getIndexById(id);
            if(exists){
                System.out.println("请输入商品名");
                String name = sc.next();
                System.out.println("请输入重量");
                String weight = sc.next();
                System.out.println("请输入价格");
                String price = sc.next();
                Goods g = new Goods(id, name, weight, price);
                service.editGoods(indexById,g);
                System.out.println("修改成功");
                break;
            }
        }

    }
    public void deleteGoods() throws IOException {
        while (true) {
            System.out.println("请输入编号");
            String id = sc.next();
            boolean exists = service.isExists(id);
            int indexById = service.getIndexById(id);
            if(exists){
                service.deleteGoods(indexById);
                System.out.println("删除成功");
                break;
            }
        }
    }

    }


